<template>
  <div class="content">
    <div class="content-left">
      <div class="left-search-item">
        <h4>年级分类</h4>
        <a-tree :tree-data="contentData.cData" :selected-keys="contentData.selectedKeys" @select="onSelect"
          style="min-height: 220px;">
        </a-tree>
      </div>
      <div class="left-search-item">
        <h4>学科标签</h4>
        <div class="tag-view tag-flex-view">
          <span class="tag" :class="{ 'tag-select': contentData.selectTagId === item.id }"
            v-for="item in contentData.tagData" :key="item.id" @click="clickTag(item.id)">{{ item.title }}</span>
        </div>
      </div>
    </div>
    <div class="content-right">
      <div class="top-select-view flex-view">
        <div class="order-view">
          <span class="title"></span>
          <span class="tab" :class="contentData.selectTabIndex === index ? 'tab-select' : ''"
            v-for="(item, index) in contentData.tabData" :key="index" @click="selectTab(index)">
            {{ item }}
          </span>
          <span :style="{ left: contentData.tabUnderLeft + 'px' }" class="tab-underline"></span>
        </div>

        <div class="location-filter">
          <a-select v-model:value="contentData.selectedLocation" placeholder="地区筛选" style="width: 150px;"
            @change="handleLocationFilter">
            <a-select-option value="">全部地区</a-select-option>
            <a-select-option v-for="location in contentData.locationOptions" :key="location" :value="location">
              {{ location }}
            </a-select-option>
          </a-select>
        </div>
      </div>
      <a-spin :spinning="contentData.loading" style="min-height: 200px;">
        <div class="pc-thing-list flex-view">
          <div v-for="item in contentData.pageData" :key="item.id" @click="handleDetail(item)"
            class="thing-item item-column-3" v-show="item.status === '0'">
            <div class="img-view">
              <img :src="item.cover">
            </div>
            <div class="info-view">
              <div class="name-score-view">
                <h3 class="thing-name">{{ item.title.substring(0, 12) }}</h3>
                <div class="rating-stars">
                  <span v-for="star in 5" :key="star" class="star">
                    <img :src="star <= Math.round(item.avgScore || 0) ? start1 : start2" class="star-icon"
                      :class="{ 'filled': star <= Math.round(item.avgScore || 0) }" />
                  </span>
                </div>
              </div>
              <span>
                <span class="a-price-symbol"></span>
                <span class="a-price">{{ item.price }}元/时</span>&nbsp;
                <span class="location-text">{{ item.location }}</span>
              </span>
            </div>
          </div>
          <div v-if="contentData.pageData.length <= 0 && !contentData.loading" class="no-data" style="">暂无数据</div>
        </div>
      </a-spin>
      <div class="page-view" style="">
        <a-pagination v-model="contentData.page" size="small" @change="changePage" :hideOnSinglePage="true"
          :defaultPageSize="contentData.pageSize" :total="contentData.total" :showSizeChanger="false" />
      </div>
    </div>
  </div>

  <div class="deepseek-float-btn" @click="showChatModal = true">
    <img :src="deepseeklog" alt="AI助手" />
  </div>

  <!-- AI聊天弹窗 -->
  <a-modal v-model:visible="showChatModal" title="DeepSeek AI助手" width="600px" :footer="null"
    :bodyStyle="{ padding: '0', height: '60vh', display: 'flex' }">
    <AiChat />
  </a-modal>
</template>

<script setup>
import { listApi as listClassificationList } from '/@/api/classification'
import { listApi as listTagList } from '/@/api/tag'
import { listApi as listThingList } from '/@/api/thing'
import { BASE_URL } from "/@/store/constants";
import { useUserStore } from "/@/store";
import Deepseeklog from '/@/assets/icons/deepseek.jpeg';
import { useRouter } from 'vue-router';
import { Modal as AModal } from 'ant-design-vue'
import AiChat from '/@/components/AiChat.vue'
import { Start1, Start2 } from '/@/assets'

const userStore = useUserStore()
const router = useRouter();
const showChatModal = ref(false)

const deepseeklog = Deepseeklog;
const start1 = Start1;
const start2 = Start2;


const contentData = reactive({
  selectX: 0,
  selectTagId: -1,
  cData: [],
  selectedKeys: [],
  tagData: [],
  loading: false,

  tabData: ['最新', '最热', '推荐'],
  selectTabIndex: 0,
  tabUnderLeft: 12,

  thingData: [],
  pageData: [],

  page: 1,
  total: 0,
  pageSize: 9,
  selectedLocation: '',      // 选中地区
  locationOptions: [],       // 地区选项
})

onMounted(() => {
  initSider()
  getThingList({})
})

const initSider = () => {
  contentData.cData.push({ key: '-1', title: '全部' })
  listClassificationList().then(res => {
    res.data.forEach(item => {
      item.key = item.id
      contentData.cData.push(item)
    })
  })
  listTagList().then(res => {
    contentData.tagData = res.data
  })
}

const getSelectedKey = () => {
  if (contentData.selectedKeys.length > 0) {
    return contentData.selectedKeys[0]
  } else {
    return -1
  }
}
const onSelect = (selectedKeys) => {
  contentData.selectedKeys = selectedKeys
  console.log(contentData.selectedKeys[0])
  if (contentData.selectedKeys.length > 0) {
    getThingList({ c: getSelectedKey() })
  }
  contentData.selectTagId = -1
}
const clickTag = (index) => {
  contentData.selectedKeys = []
  contentData.selectTagId = index
  getThingList({ tag: contentData.selectTagId })
}

// 最新|最热|推荐
const selectTab = (index) => {
  contentData.selectTabIndex = index
  contentData.tabUnderLeft = 12 + 50 * index
  console.log(contentData.selectTabIndex)
  let sort = (index === 0 ? 'recent' : index === 1 ? 'hot' : 'recommend')
  const data = { sort: sort }
  if (contentData.selectTagId !== -1) {
    data['tag'] = contentData.selectTagId
  } else {
    data['c'] = getSelectedKey()
  }
  getThingList(data)
}
const handleDetail = (item) => {
  // 跳转新页面
  let text = router.resolve({ name: 'detail', query: { id: item.id } })
  window.open(text.href, '_blank')
}
// 分页事件

const changePage = (page) => {
  contentData.page = page;
  let filteredData = contentData.thingData;

  // 应用所有筛选条件
  if (contentData.selectedLocation) {
    filteredData = filteredData.filter(item => 
      item.location === contentData.selectedLocation
    );
  }

  // 应用状态筛选（保留原有状态过滤）
  filteredData = filteredData.filter(item => item.status === '0');

  contentData.total = filteredData.length;
  
  const start = (contentData.page - 1) * contentData.pageSize;
  contentData.pageData = filteredData.slice(start, start + contentData.pageSize);
}

// 在获取数据时增加状态筛选
const getThingList = (data) => {
  contentData.loading = true;
  listThingList(data).then(res => {
    contentData.loading = false;
    
    // 添加状态过滤
    const filteredData = res.data.filter(item => item.status === '0');
    
    // 更新数据
    filteredData.forEach(item => {
      if (item.cover) item.cover = BASE_URL + '/api/staticfiles/image/' + item.cover;
    });

    contentData.thingData = filteredData;  // 存储已过滤的数据
    contentData.locationOptions = [...new Set(filteredData.map(item => item.location))];
    changePage(1);  // 重置分页
  }).catch(err => {
    contentData.loading = false;
  });
}
// 处理地区筛选
const handleLocationFilter = (value) => {
  contentData.selectedLocation = value
  changePage(1) // 重置到第一页
}

</script>

<style scoped lang="less">
.content {
  display: flex;
  flex-direction: row;
  width: 1100px;
  margin: 80px auto;
}

.content-left {
  width: 220px;
  margin-right: 32px;
}

.left-search-item {
  overflow: hidden;
  border-bottom: 1px solid #cedce4;
  margin-top: 24px;
  padding-bottom: 24px;
}

h4 {
  color: #4d4d4d;
  font-weight: 600;
  font-size: 16px;
  line-height: 24px;
  height: 24px;
}

.category-item {
  cursor: pointer;
  color: #333;
  margin: 12px 0 0;
  padding-left: 16px;
}

ul {
  margin: 0;
  padding: 0;
}

ul {
  list-style-type: none;
}

li {
  margin: 4px 0 0;
  display: list-item;
  text-align: -webkit-match-parent;
}

.child {
  color: #333;
  padding-left: 16px;
}

.child:hover {
  color: #4684e2;
}

.select {
  color: #4684e2;
}

.flex-view {
  -webkit-box-pack: justify;
  -ms-flex-pack: justify;
  //justify-content: space-between;
  display: flex;
}

.name {
  font-size: 14px;
}

.name:hover {
  color: #4684e2;
}

.count {
  font-size: 14px;
  color: #999;
}

.check-item {
  font-size: 0;
  height: 18px;
  line-height: 12px;
  margin: 12px 0 0;
  color: #333;
  cursor: pointer;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
}

.check-item input {
  cursor: pointer;
}

.check-item label {
  font-size: 14px;
  margin-left: 12px;
  cursor: pointer;
  -webkit-box-flex: 1;
  -ms-flex: 1;
  flex: 1;
}

.tag-view {
  -ms-flex-wrap: wrap;
  flex-wrap: wrap;
  margin-top: 4px;
}

.tag-flex-view {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
}

.tag {
  background: #fff;
  border: 1px solid #a1adc6;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  border-radius: 16px;
  height: 20px;
  line-height: 18px;
  padding: 0 8px;
  margin: 8px 8px 0 0;
  cursor: pointer;
  font-size: 12px;
  color: #152833;
}

.tag:hover {
  background: #4684e3;
  color: #fff;
  border: 1px solid #4684e3;
}

.tag-select {
  background: #4684e3;
  color: #fff;
  border: 1px solid #4684e3;
}

.content-right {
  -webkit-box-flex: 1;
  -ms-flex: 1;
  flex: 1;
  padding-top: 12px;

  .pc-search-view {
    margin: 0 0 24px;
    -webkit-box-align: center;
    -ms-flex-align: center;
    align-items: center;

    .search-icon {
      width: 20px;
      height: 20px;
      -webkit-box-flex: 0;
      -ms-flex: 0 0 20px;
      flex: 0 0 20px;
      margin-right: 16px;
    }

    input {
      outline: none;
      border: 0px;
      -webkit-box-flex: 1;
      -ms-flex: 1;
      flex: 1;
      border-bottom: 1px solid #cedce4;
      color: #152844;
      font-size: 14px;
      height: 22px;
      line-height: 22px;
      -ms-flex-item-align: end;
      align-self: flex-end;
      padding-bottom: 8px;
    }

    .clear-search-icon {
      position: relative;
      left: -20px;
      cursor: pointer;
    }

    button {
      outline: none;
      border: none;
      font-size: 14px;
      color: #fff;
      background: #288dda;
      border-radius: 32px;
      width: 88px;
      height: 32px;
      line-height: 32px;
      margin-left: 2px;
      cursor: pointer;
    }

    .float-count {
      color: #999;
      margin-left: 24px;
    }
  }

  .flex-view {
    display: flex;
  }

  .top-select-view {
    -webkit-box-pack: justify;
    -ms-flex-pack: justify;
    justify-content: space-between;
    -webkit-box-align: center;
    -ms-flex-align: center;
    align-items: center;
    height: 40px;
    line-height: 40px;

    .type-view {
      position: relative;
      font-weight: 400;
      font-size: 18px;
      color: #5f77a6;

      .type-tab {
        margin-right: 32px;
        cursor: pointer;
      }

      .type-tab-select {
        color: #152844;
        font-weight: 600;
        font-size: 20px;
      }

      .tab-underline {
        position: absolute;
        bottom: 0;
        //left: 22px;
        width: 16px;
        height: 4px;
        background: #4684e2;
        -webkit-transition: left .3s;
        transition: left .3s;
      }


    }

    .order-view {
      position: relative;
      color: #6c6c6c;
      font-size: 14px;

      .title {
        margin-right: 8px;
      }

      .tab {
        color: #999;
        margin-right: 20px;
        cursor: pointer;
      }

      .tab-select {
        color: #152844;
      }

      .tab-underline {
        position: absolute;
        bottom: 0;
        left: 84px;
        width: 16px;
        height: 4px;
        background: #4684e2;
        -webkit-transition: left .3s;
        transition: left .3s;
      }
    }

  }

  .pc-thing-list {
    -ms-flex-wrap: wrap;
    flex-wrap: wrap;
    gap: 20px; // 替换margin-right实现等间距
    margin: 0 -10px; // 抵消gap产生的边缘间距

    .thing-item {
      min-width: 255px;
      max-width: 255px;
      position: relative;
      flex: 1;
      height: fit-content;
      overflow: hidden;
      margin: 7px 0; // 简化margin
      cursor: pointer;
      transition: all 0.3s ease;
      background: #fff;
      border-radius: 8px;
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
      padding: 12px;

      &:hover {
        transform: translateY(-4px);
        box-shadow: 0 6px 16px rgba(0, 0, 0, 0.12);

        .img-view img {
          transform: scale(1.05);
        }
      }

      .img-view {
        height: 200px;
        width: 100%;
        border-radius: 4px;
        overflow: hidden;

        img {
          width: 100%;
          height: 100%;
          object-fit: cover;
          transition: transform 0.3s ease;
        }
      }

      .info-view {
        padding: 16px 8px 8px;

        .thing-name {
          font-size: 15px;
          line-height: 1.4;
          margin: 0 0 8px;
          color: #152844;
          font-weight: 500;
          height: 42px;
          display: -webkit-box;
          -webkit-line-clamp: 2;
          -webkit-box-orient: vertical;
          overflow: hidden;
          padding: 4px 0 0 0;
        }

        .a-price {
          font-size: 16px;
          color: #3dbeff;
          font-weight: 500;

          &-symbol {
            font-size: 12px;
          }
        }

        .location-text {
          color: #7f8c8d;
          font-size: 14px;
        }
      }

      // 价格与地址布局
      span>span:last-child {
        float: right;
        color: #666;
        font-size: 12px;
      }
    }

    .no-data {
      height: 200px;
      line-height: 200px;
      text-align: center;
      width: 100%;
      font-size: 16px;
      color: #152844;
    }
  }

  .page-view {
    width: 100%;
    text-align: center;
    margin-top: 48px;
  }
}

.a-price-symbol {
  top: -0.5em;
  font-size: 12px;
}

.a-price {
  color: #0F1111;
  font-size: 14px;
}

.deepseek-float-btn {
  position: fixed;
  left: 60px;
  bottom: 60px;
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  cursor: pointer;
  transition: all 0.3s ease;
  z-index: 1000;

  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    object-fit: cover;
  }

  &:hover {
    transform: scale(1.1);
    box-shadow: 0 6px 16px rgba(0, 0, 0, 0.2);
  }
}

.location-filter {
  margin-left: auto;
  display: flex;
  align-items: center;
  position: relative;
  z-index: 10;

  // 修复深度选择器语法
  :deep(.ant-select) {
    &:hover .ant-select-selector {
      border-color: #4684e3 !important;
    }

    .ant-select-selector {
      border-radius: 20px !important;
      padding: 0 15px !important;
      height: 32px !important;
      border-color: #cedce4 !important;
      transition: all 0.3s ease;
      box-shadow: none !important;
      background: #f8fafc;

      .ant-select-selection-item {
        font-size: 14px;
        color: #152844;
        padding-right: 20px !important;
      }
    }


    .ant-select-arrow {
      color: #4684e3;
      right: 12px;
    }
  }

  // 下拉菜单样式
  :deep(.ant-select-dropdown) {
    border-radius: 8px !important;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1) !important;
    padding: 8px 0;

    .ant-select-item {
      padding: 8px 16px !important;
      font-size: 14px;
      transition: all 0.2s;

      &-option-active:not(&-option-disabled) {
        background: #f5f8ff !important;
      }

      &-option-selected {
        background: #f0f7ff !important;
        font-weight: 500;
        color: #4684e3 !important;
      }
    }
  }
}

:deep(.custom-chat-modal) {
  .ant-modal-content {
    border-radius: 16px !important;
    overflow: hidden;

    .ant-modal-header {
      border-radius: 16px 16px 0 0;
      background: #f8f9fa;
      border-bottom: none;
      padding: 16px 24px;
    }

    .ant-modal-body {
      border-radius: 0 0 16px 16px;
    }

    .ant-modal-title {
      color: #1890ff;
      font-weight: 500;
    }
  }
}

.name-score-view {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.rating-stars {
  display: inline-flex;
  align-items: center;
  margin-left: 10px;
  margin: 0 0 25px;
  height: 40px;
}

.star {
  display: inline-block;
  margin-right: 2px;
}

.star-icon {
  width: 20px;
  height: 20px;
  vertical-align: middle;
  transition: all 0.3s;
}

.filled {
  filter: brightness(1.2);
}
</style>
